package com.yuelan.codelib.utils;

import org.json.JSONObject;

import com.yuelan.codelib.utils.FileUtil;
import com.yuelan.codelib.utils.TextUtil;

import android.content.Context;

public class CacheFileUtil {
	public static final String CaCheName = "cache.info";

	public static void putString(String key, String value, Context c) {
		try {
			JSONObject job = null;
			String json = FileUtil.readDateFile(CaCheName, c);
			if (TextUtil.notNull(json)) {
				job = new JSONObject(json);
				job.put(key, value);
			} else {
				job = new JSONObject();
				job.put(key, value);
			}
			FileUtil.writeDateFile(CaCheName, job.toString().getBytes("utf-8"),
					c);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void putInt(String key, int value, Context c) {
		try {
			JSONObject job = null;
			String json = FileUtil.readDateFile(CaCheName, c);
			if (TextUtil.notNull(json)) {
				job = new JSONObject(json);
				job.put(key, value);
			} else {
				job = new JSONObject();
				job.put(key, value);
			}
			FileUtil.writeDateFile(CaCheName, job.toString().getBytes("utf-8"),
					c);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static int getInt(String key, Context c) {
		int value = 0;
		try {
			JSONObject job = null;
			String json = FileUtil.readDateFile(CaCheName, c);
			if (TextUtil.notNull(json)) {
				job = new JSONObject(json);
				if (job.has(key)) {
					value = job.getInt(key);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	public static String getString(String key, Context c) {
		String value = null;
		try {
			JSONObject job = null;
			String json = FileUtil.readDateFile(CaCheName, c);
			if (TextUtil.notNull(json)) {
				job = new JSONObject(json);
				if (job.has(key)) {
					value = job.getString(key);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	public static String getString(String key, String defultvalue, Context c) {
		String value = defultvalue;
		try {
			JSONObject job = null;
			String json = FileUtil.readDateFile(CaCheName, c);
			if (TextUtil.notNull(json)) {
				job = new JSONObject(json);
				if (job.has(key)) {
					value = job.getString(key);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
}
